package com.demo.admin;





import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.demo.admin.AdminInterceptor;
import com.demo.admin.AdminValidator;
import com.demo.admin.ContentValidator;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.demo.common.model.Admin;
import com.demo.common.model.Category;
import com.demo.common.model.Log;
import com.demo.common.model.Content;


@Before(AdminInterceptor.class)
public class AdminController extends Controller{
	
	
	//首页
	public void main(){
		render("main.html");
	}
	//帮助页面
	public void help(){
		render("help.html");
	}
	//登陆日志
	public void log(){
		Admin admin = new Admin();
		List<Log> list = admin.getLog();
		this.setAttr("log",list);
		setAttr("log_display","log_display"); 
		render("log.html");
	}
	//相册
	public void gallery(){
		render("gallery.html");
	}
	//404 
	public void notfound(){
		render("notfound.html");
	}
	
	
	
	
	//个人资料
	public void user(){
		String email = getSessionAttr("email");
		Admin admin = new Admin();
		setAttr("user",admin.getUser(email)); 
		setAttr("user_display","user_display"); 
		render("user.html");
	}
	//更新个人资料
	public void updateuser(){
		getModel(Admin.class).update();
		redirect("/admin/user");
	}
	//更新个人头像
	public void avatar(){
		
		String file = System.currentTimeMillis()+".jpg";
		UploadFile uf = getFile();
		uf.getFile().renameTo(new File("D:\\WEB\\jfinal_demo\\WebRoot\\upload\\"+file));
		
		Record admin = Db.findById("admin", getPara("admin.id")).set("avatar",file); 
		Db.update("admin", admin);  
		
		redirect("/admin/user");
	}
	//修改密码
	public void updatepsw(){	
		String password=getPara("admin.password");
		int id = Integer.parseInt(getPara("admin.id"));
		Admin admin = new Admin();
		String pswReturn = admin.upPsw(password, id);
		setAttr("pswReturn",pswReturn); 
		
		String email = getSessionAttr("email");
		//Admin admin = new Admin();
		setAttr("user",admin.getUser(email)); 
		setAttr("user_display","user_display"); 
		render("user.html");
	}
	
	
	
	//文章列表
	public void table(){	
		Category category = new Category();
		List<Category> categorylist = category.getCategory();
		this.setAttr("categorylist",categorylist);
		
		setAttr("tablePage", Content.dao.paginateAll(getParaToInt(0, 1), 10));
		setAttr("table_display","table_display"); 
		render("table.html");
	}
	//发表文章
	public void form(){
		Category category = new Category();
		List<Category> categorylist = category.getCategory();
		this.setAttr("categorylist",categorylist);
		
		Date date=new Date();
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time=format.format(date);
		setAttr("time",time); 
		setAttr("form_display","form_display"); 
		render("form.html");
	}
	//保存文章
	@Before(ContentValidator.class)
	public void savecontent() {	
		getModel(Content.class).save();
		redirect("/admin/table");
	}
	//删除文章
	public void deletArticle() {
		Content.dao.deleteById(getParaToInt());
		redirect("/admin/table");
	}
	//编辑文章
	public void editArticle() {
		Category category = new Category();
		List<Category> categorylist = category.getCategory();
		this.setAttr("categorylist",categorylist);
		
		Date date=new Date();
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time=format.format(date);
		setAttr("time",time); 
		setAttr("content", Content.dao.findById(getParaToInt()));
		setAttr("table_display","table_display");
	}
	//更新文章
	@Before(ContentValidator.class)
	public void updateArticle() {	
		getModel(Content.class).update();
		redirect("/admin/table");
	}
	
	
	//异步上传文章封面
	public void imageUpload() {
		String file = System.currentTimeMillis()+".jpg";
		UploadFile uf = getFile();
		uf.getFile().renameTo(new File("D:\\WEB\\jfinal_demo\\WebRoot\\upload\\"+file));
		
		JSONObject json = new JSONObject();
		json.put("message", file);
		renderJson(json.toJSONString());
		
	} 
	//根据分类查询文章
	public void searchArticle() {
		Category category = new Category();
		List<Category> categorylist = category.getCategory();
		this.setAttr("categorylist",categorylist);
		
		String cname = getPara("cname");
		setAttr("cname",cname); 
		setAttr("tablePage", Content.dao.paginate(getParaToInt(0, 1), 10,cname));
		setAttr("table_display","table_display"); 
		render("table.html");
	}
	
	
	
	//分类列表
	public void classlist(){
		setAttr("blogPage", Category.dao.paginate(getParaToInt(0, 1), 10));
		setAttr("classlist_display","classlist_display"); 
		render("classlist.html");
	}
	//添加分类
	public void addclass(){
		setAttr("addclass_display","addclass_display"); 
		render("addclass.html");
	}
	//保存分类
	@Before(AdminValidator.class)
	public void savecategory() {
		getModel(Category.class).save();
		redirect("/admin/classlist");
	}
	//删除分类
	public void deletecategory() {
		Category.dao.deleteById(getParaToInt());
		redirect("/admin/classlist");
	}
	//编辑分类
	public void editcategory() {
		setAttr("category", Category.dao.findById(getParaToInt()));
		setAttr("classlist_display","classlist_display"); 
	}
	//更新分类
	@Before(AdminValidator.class)
	public void updatecategory() {
		getModel(Category.class).update();
		redirect("/admin/classlist");
	}
	
	
	
	
	
	
}

